Macromedia Flash Article

Bike Trips Sample Application: Using the XMLConnector Component in Flash MX Professional 2004


Table of Contents

  1. Introduction
  2. Getting Started: Examining the Bike Trips Application and Its Data
  3. Configuring the Application to Use the XMLConnector Component
  4. Binding XML Data to a ComboBox Component
  5. Creating ComboBox Labels Using XML Data
  6. Displaying and Updating a Trip Description in a TextArea Component
  7. Displaying and Updating a Formatted Cost in a TextInput Component
  8. Displaying and Updating a Graphic in a Loader Component
  9. Summary and Related Articles and Resources

Configuring the Application to Use the XMLConnector Component

To use the XMLConnector component, you place a component instance in your application, point it at the data source XML file, and add a behavior that triggers the component to load data as the application loads.

  1. Open XMLConnector _raw.fla in Flash MX 2004 and save it as XMLConnector_step1.fla.
  2. Take a look at the layer structure; note that only the background and static text layers have any content.

    Note: Although the document includes a code layer, you will add code as a behavior, so you won't have to hand code. Nonetheless, for good file organization, it's a recommended practice to attach code to a distinct layer, rather than to visual content layers.

  3. Open the Components panel and drag and drop an instance of the XMLConnector component into your application, placing it just offstage to the left of the static TextField containing the words Bike Trip Central.

    Dragging the XMLConnector component into your application.

    Figure 1. Dragging the XMLConnector component into your application.

    Note: The XMLConnector component would be invisible at runtime if placed on the Stage, but it might interfere with other visual development. So, I recommended that you place it offstage.

  4. With the XMLConnector component selected, open the Property inspector, click the Parameters tab, and set the following values:

    • Instance Name: xcTrips
    • URL: data/trips.xml
    • Direction: receive
  5. Leave the remaining properties at their default settings:

    • ignoreWhite: true
    • multipleSimultaneousAllowed: true
    • suppressInvalidCalls: false

    Note: In general, ignore white space when reading XML, as you may not be able to control whether your source XML includes white space for readability (as does trips.xml) or does not. The other two settings are relevant to sending XML data through the component, which you will not do in this tutorial.

  6. Add a Trigger Data Source behavior to the first frame of the code layer, to trigger the XMLConnector component to load its data:

    1. Select the first frame of the code layer.
    2. Open the Behaviors panel.
    3. Press the Plus (+) button to add a new behavior.
    4. Select Data > Trigger Data Source.
    5. Select the xcTrips component from the Select Data Source component dialog box.

    This behavior adds the following line of code to the code layer:

    this.xcTrips.trigger();

    Behaviors are a new Flash MX 2004 feature. They attach prewritten code to your application. This particular behavior causes the XMLConnector component, which you named xcTrips, to execute its trigger() method and load its data from trips.xml when the playhead executes the first frame of this one-frame application. Once you add the behavior, you will see a lowercase "a" appear in the frame, indicating that code is attached to it.

  7. Test your application to verify data is loading:

    1. Select Control > Test Movie to test the application.
    2. Select Debug > List Variables to examine variables available in the application.
    3. Press Control-F and search for the variable called:

      level0.xcTrips.results

    4. Examine the XML data structure in this variable; note that it matches the values in trips.xml.
  8. Import data/trips.xml as the schema for xcTrips:

    1. Select xcTrips.
    2. Open the Component Inspector panel.
    3. Select the Schema tab.
    4. Select the results:XML property.
    5. Press the "Import a schema from a sample XML file" button, in the upper right corner of the Schema tab.
    6. Open the /data directory, select trips.xml, and press Open.

      Importing data/trips.xml as the Schema for xcTrips.

      Figure 2. Importing data/trips.xml as the schema for xcTrips.

  9. Using the Name tabs above the Stage, toggle between XMLConnector_step1.fla and data/trips.xml. Compare the imported data schema in results:XML property of xcTrips to the XML data in data/trips.xml. Notice the following:

    XML file: data/trips.xml Component Inspector panel schema for xcTrips (imported from data/trips.xml)
    The entire XML packet in data/trips.xml is wrapped in a single trips tag. The root of the imported schema is a property called trips of the data type Object (represented as trips:Object).
    The XML packet contains a series of sections—or nodes—each called trip. The trips object contains a property called trip, which is a series—or Array—of an indefinite number of objects.
    Each trip node has an attribute called name plus four nodes nested within it: description, cost, tapes, and image. Each object in trip:Array has five properties: name, description, cost, tapes, and image. Name is an attribute of the object (signified by the @ symbol); three properties have string data, and two have integer (numeric) data.
  10. Save your changes to XMLConnector_step1.fla.